package com.qys.livesMall.event.listener;

import com.google.common.collect.Lists;
import com.qys.livesMall.anchor.event.GiveLikeEvent;
import com.qys.livesMall.anchor.event.pojo.GiveLikeEventPOJO;
import com.qys.livesMall.anchor.model.entity.AnchorLivesRoom;
import com.qys.livesMall.anchor.model.pojo.GiveLikeCachePOJO;
import com.qys.livesMall.anchor.model.pojo.child.GiveLikeChild;
import com.qys.livesMall.anchor.service.AnchorLivesRoomService;
import com.qys.livesMall.anchor.service.H2AnchorService;
import com.qys.livesMall.app.user.service.IH2AppUserService;
import com.qys.livesMall.cache.RedisCache;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

import static com.qys.livesMall.anchor.constants.RedisConstant.LIVE_ROOM_ONE_HOURS_LIKE_LIST;

@Slf4j
@Component
public class GiveLikeListener implements ApplicationListener<GiveLikeEvent> {

    @Resource
    private  RedisCache redisCache;
    @Resource
    private AnchorLivesRoomService anchorLivesRoomService;
    @Resource
    private H2AnchorService anchorService;
    @Resource
    private IH2AppUserService appUserService;


    @Async
    @Override
    public void onApplicationEvent(GiveLikeEvent event) {
        GiveLikeEventPOJO pojo = (GiveLikeEventPOJO) event.getSource();

        AnchorLivesRoom livesRoom = anchorLivesRoomService.queryByRoomNum(pojo.getLivesRoomNum());
        log.info("监听到用户【{}】,在直播间【{}】给主播点赞【{}】次",
                pojo.getUserId(), pojo.getLikeNum(), pojo.getLikeNum());

        //获取各直播间数据集合
        List<GiveLikeCachePOJO> list = redisCache.getCacheList(getLiveListKey());

        //获取当前直播间数据
        List<GiveLikeCachePOJO> collect = list.stream().filter(e -> e.getLivesRoomNum().equals(pojo.getLivesRoomNum())).toList();

        if(!CollectionUtils.isEmpty(collect)){
            //查询直播间点赞数据
            GiveLikeCachePOJO likeCache = collect.get(0);
            /***排除一小时之前的数据*/
            List<GiveLikeChild> cacheList = likeCache.getChildren().stream().filter(e -> e.getCreateTime().plusHours(1).isAfter(LocalDateTime.now())).collect(Collectors.toList());
            GiveLikeChild child = new GiveLikeChild();
            child.setCreateTime(LocalDateTime.now());
            child.setLikeNum(pojo.getLikeNum());
            cacheList.add(child);
            likeCache.setChildren(cacheList);

            Integer oneHoursLikeNum = likeCache.getChildren().stream().mapToInt(GiveLikeChild::getLikeNum).sum();
            likeCache.setOneHoursLikeNum(oneHoursLikeNum);
        } else {
            GiveLikeCachePOJO giveLikeCachePOJO = new GiveLikeCachePOJO();
            giveLikeCachePOJO.setLivesRoomNum(pojo.getLivesRoomNum());
            giveLikeCachePOJO.setAnchorName(anchorLivesRoomService.getUserByAnchorId(livesRoom.getAnchorId()).getNickName());
            Long userId = anchorService.getById(livesRoom.getAnchorId()).getUserId();
            giveLikeCachePOJO.setAnchorAvatar(appUserService.getById(userId).getAvatar());
            giveLikeCachePOJO.setAnchorId(userId);

            GiveLikeChild likeChild = new GiveLikeChild();
            likeChild.setLikeNum(pojo.getLikeNum());
            likeChild.setCreateTime(LocalDateTime.now());
            giveLikeCachePOJO.setChildren(Lists.newArrayList(likeChild));
            Integer oneHoursLikeNum = giveLikeCachePOJO.getChildren().stream().mapToInt(GiveLikeChild::getLikeNum).sum();
            giveLikeCachePOJO.setOneHoursLikeNum(oneHoursLikeNum);
            list.add(giveLikeCachePOJO);
        }

        /***储存一个小时数据*/
        redisCache.deleteObject(getLiveListKey());
        redisCache.setCacheList(getLiveListKey(), list);
    }

    private String getLiveListKey() {
        return LIVE_ROOM_ONE_HOURS_LIKE_LIST;
    }


}
